﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using testapi.Services;
using testapi.Models;
using Microsoft.EntityFrameworkCore;

namespace testapi.Controllers
{
    [Route("[controller]")]
    public class ControllerBase<T> : Controller where T:class,IEid
    {
        protected ServiceContext ServiceContext { get; private set; }
        public ControllerBase(ServiceContext serviceContext)
        {
            ServiceContext = serviceContext;
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        public async Task<IActionResult> Get()
        {
            return Ok(await ServiceContext.DbContext.Set<T>().Where(t=>t.Flag==0).ToListAsync());
        }

        /// <summary>
        /// 通过Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id:int}")]
        public async Task<IActionResult> GetById(int id)
        {
            return Ok(await ServiceContext.DbContext.Set<T>().Where(t=>t.Id==id && t.Flag==0).FirstOrDefaultAsync());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut()]
        public async Task<IActionResult> Put([FromBody] T model)
        {
            if (model == null) return null;

            var oldEntity =await ServiceContext.DbContext.Set<T>().Where(t => t.Id == model.Id).FirstOrDefaultAsync();

            UpdateBefore(oldEntity, model);

            ServiceContext.DbContext.Set<T>().Update(oldEntity);
         var i= await ServiceContext.DbContext.SaveChangesAsync();

            return Ok(i.ToString());
        }
  

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost()]
        public async Task<IActionResult> Post([FromBody] T model)
        {
            if (model == null) return null;

            model.Flag = 0;

            ServiceContext.DbContext.Set<T>().Add(model);
            var i = await ServiceContext.DbContext.SaveChangesAsync();

            return Created("", i.ToString());
        }

        /// <summary>
        /// 逻辑修改的功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("Delete/{id:int}")]
        public async Task<IActionResult> Delete(int id)
        {
            var oldentity = await ServiceContext.DbContext.Set<test>().Where(t => t.Id == id).FirstOrDefaultAsync();

               oldentity.Flag = 1;
               var result = ServiceContext.DbContext.Set<test>().Update(oldentity);
              var i=  await ServiceContext.DbContext.SaveChangesAsync();
               return Ok(i.ToString());
        }

        protected virtual T UpdateBefore(T oldEntity, T model)
        {
            return oldEntity;
        }
    }
}
